<template>
  <div class="content">
    <el-button @click="openFormDialog(form,'add')">发布新版</el-button>
    <div style="display: flex;flex-direction: column;align-items: flex-end;">

      <el-table
          :data="versionList"
          row-key="id"
          stripe
          :row-style="{height:'40px'}"
          :cell-style="{padding:'0px'}"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column
            label="平台">
          <template slot-scope="scope">
            <el-tag> {{scope.row.type===1101?'安卓':'苹果'}} </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="versionCode"
            label="版本号">
        </el-table-column>
        <el-table-column
            prop="versionName"
            label="版本名称">
        </el-table-column>
        <el-table-column
            prop="versionInfo"
            label="版本信息">
        </el-table-column>
        <el-table-column
            label="更新类型">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.updateType==='forcibly'">强制更新</el-tag>
            <el-tag v-if="scope.row.updateType==='solicit'">弹框更新</el-tag>
            <el-tag v-if="scope.row.updateType==='silent'">静默更新</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="downloadUrl"
            label="下载地址">
        </el-table-column>
        <el-table-column
            prop="createTime"
            label="创建时间">
        </el-table-column>
        <el-table-column
            prop="updateTime"
            label="更新时间">
        </el-table-column>
        <el-table-column
            label="操作"
            width="200">
          <template slot-scope="scope">
            <el-button @click="openFormDialog(scope.row,'edit')" type="text" size="small">编辑</el-button>
            <el-button @click="deleteById(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination style="margin-top: 5px"
                     @size-change="currentSizeChange"
                     @current-change="currentChange"
                     :current-page="currentPage"
                     :page-sizes="[10, 20, 30, 40]"
                     :page-size="limit"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>

      <el-dialog title="发布新版" :visible.sync="dialogFormVisible" width="600px">
        <el-form :model="form" ref="form">
          <el-form-item label="版本号" prop="versionCode" :label-width="formLabelWidth"
                        :rules="[
                                      { required: true, message: '请填写完整'}
                                    ]">
            <el-input type="number" v-model="form.versionCode" autocomplete="off"/>
          </el-form-item>
          <el-form-item label="版本名称" prop="versionName" :label-width="formLabelWidth"
                        :rules="[
                                      { required: true, message: '请填写完整'}
                                    ]">
            <el-input v-model="form.versionName" autocomplete="off"/>
          </el-form-item>
          <el-form-item label="版本信息" prop="versionInfo" :label-width="formLabelWidth"
                        :rules="[
                                      { required: true, message: '请填写完整'}
                                    ]">
            <el-input type="textarea" :rows="5" v-model="form.versionInfo" autocomplete="off"/>
          </el-form-item>
          <el-form-item label="更新类型" prop="updateType" :label-width="formLabelWidth"
                        :rules="[
                                      { required: true, message: '请填写完整'}
                                    ]">
            <el-select v-model="form.updateType" placeholder="请选择">
              <el-option
                  v-for="item in updateTypeList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="下载链接" prop="downloadUrl" :label-width="formLabelWidth"
                        :rules="[
                                      { required: true, message: '请填写完整'}
                                    ]">
            <el-input v-model="form.downloadUrl" autocomplete="off"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        </div>
      </el-dialog>

    </div>

  </div>
</template>

<script>
export default {
  name: "VersionList",
  components: {

  },
  data(){
    return{
      page: 1,
      limit: 10,
      total: 0,
      currentPage:1,
      ofType:'',
      versionList: [],
      form:{
        id:'',
        versionCode:'',
        versionName:'',
        versionInfo:'',
        updateType:'',
        downloadUrl:''
      },
      updateTypeList:[
        {
          name:'强制更新',
          value:'forcibly'
        },
        {
          name:'弹框更新',
          value:'solicit'
        },
        {
          name:'静默更新',
          value:'silent'
        }
      ],
      dialogFormVisible:false,
      formLabelWidth:'120px'
    }
  },
  created(){
    this.getVersions()
  },
  methods:{
    //获取权限列表
    getVersions(){
      let url = 'app/version/'+this.page+"/"+this.limit;
      this.$http.get(url).then(res => {
        this.total = res.data.data.data.total
        this.versionList = res.data.data.data.records
      })
    },
    //页面发生改变
    currentChange(value){
      this.page = value
      this.getVersions()
    },
    currentSizeChange(value){
      this.limit = value
      this.getVersions()
    },
    //提交表单
    submitForm(formName){
      if(this.form.id){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url = "app/version"
            this.$http.put(url,this.form).then(res=>{
              if(res.data.code===0){
                this.getVersions()
                this.dialogFormVisible = false
                this.$message({
                  type: 'success',
                  message: '修改成功!'
                });
              }
            })
          } else {
            return false;
          }
        });
      }else{
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url = "app/version"
            this.$http.post(url,this.form).then(res=>{
              if(res.data.code===0){
                this.getVersions()
                this.dialogFormVisible = false
                this.$message({
                  type: 'success',
                  message: '添加成功!'
                });
              }
            })
          } else {
            return false;
          }
        });
      }
    },
    //根据ID删除
    deleteById(row) {
      this.$confirm('确定要删除这条数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = 'app/version/'+row.id
        this.$http.delete(url).then(res=>{
          if(res.data.code===0){
            this.getVersions()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //打开编辑框
    openFormDialog(row,type){
      this.ofType = type
      this.dialogFormVisible = true;
      //添加菜单
      if(type==='add'){
        this.form = {}
      }
      //编辑菜单
      if(type==='edit'){
        if(!row.parentId){
          this.ofType = 'add'
        }
        this.form = {...row}
      }
    },
    //清空表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>
